<?php

namespace app\models;

use Yii;
use app\models\User;
use app\models\Address;
use app\models\Product;
/**
 * This is the model class for table "{{%order}}".
 *
 * @property string $orderid
 * @property string $userid
 * @property string $addressid
 * @property string $amount
 * @property string $status
 * @property string $expressid
 * @property string $expressno
 * @property string $tradeno
 * @property string $tradeext
 * @property string $createtime
 * @property string $updatetime
 */
class Order extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    const CREATEORDER=0;
    CONST CHECKORSER=100;
    const PAYFAILED=201;
    const PAYSUCCESS=202;
    const SENDED=220;
    const RECEIVED=260;

    public static $statcs=[
        self::CREATEORDER=>'订单初始化',
        self::CHECKORSER=>'待支付',
        self::PAYFAILED=>'支付失败',
        self::PAYSUCCESS=>'支付成功',
        self::SENDED=>'已发货',
        self::RECEIVED=>'订单完成',
    ];
    
    public $products;
    public $zhstatus;//状态说明
    public $username;
    public $address;

    public static function tableName()
    {
        return '{{%order}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['userid', 'addressid', 'status', 'expressid', 'createtime','amount'], 'safe'],
            ['expressno','required','message'=>'快递单号不能为空','on'=>'send'],
//            [['amount'], 'number'],
//            [['tradeext'], 'string'],
//            [['updatetime'], 'safe'],
//            [['expressno'], 'string', 'max' => 50],
//            [['tradeno'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'orderid' => 'Orderid',
            'userid' => 'Userid',
            'addressid' => 'Addressid',
            'amount' => 'Amount',
            'status' => 'Status',
            'expressid' => 'Expressid',
            'expressno' => '快递单号',
            'tradeno' => 'Tradeno',
            'tradeext' => 'Tradeext',
            'createtime' => 'Createtime',
            'updatetime' => 'Updatetime',
        ];
    }
    
    public static function  getDetail($data)
    {
        foreach ($data as $order){
            $order= self::getOrder($order);
        }
        return $data;
    }
    
    public static function getOrder($order)
    {
            $details= OrderDetail::find()->where('orderid=:oid',[':oid'=>$order->orderid])->all();
            $products=[];
            foreach ($details as $detail){
                $product= Product::find()->where('productid=:pid',[':pid'=>$detail->productid])->one();
                $product->num=$detail->productnum;
                $products[]=$product;
            }
            $order->products=$products;
            $order->username= User::find()->where('userid=:uid',[':uid'=>$order->userid])->one()->username;
            $order->address=Address::find()->where('addressid=:aid',[':aid'=>$order->addressid])->one();
            if(empty($order)){
                $order->address="";
            } else {
                $order->address=$order->address->address."，".$order->address->postcode."，".$order->address->telephone;
            }
            $order->zhstatus= self::$statcs[$order->status];
            return $order;
    }
    
    public static function getAllOrder($userid)
    {
        $orders= self::find()->where('userid=:uid',[':uid'=>$userid])->all();
        foreach ($orders as $order){
            $details= OrderDetail::find()->where('orderid=:oid',[':oid'=>$order->orderid])->all();
            $products=[];
            foreach ($details as $detail){
                $product= Product::find()->where('productid=:pid',[':pid'=>$detail->productid])->one();
                $product->num=$detail->productnum;
                $product->price=$detail->price;
                $product->cate= Category::find()->where('cateid=:cid',[':cid'=>$product->cateid])->one()->title;
                $products[]=$product;
            }
            $order->zhstatus= self::$statcs[$order->status];
            $order->products=$products;
        }
        return $orders;
    }
}
